<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发票类型" prop="invoiceType">
        <el-select v-model="queryParams.invoiceType" placeholder="请选择发票类型" clearable>
          <el-option
            v-for="dict in dict.type.c_invoice"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
            <el-form-item label="公司抬头名称" prop="invoiceTitle">
              <el-input
                v-model="queryParams.invoiceTitle"
                placeholder="请输入抬头名称"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>

      <el-form-item label="发票税号" prop="invoiceTaxNo">
        <el-input
          v-model="queryParams.invoiceTaxNo"
          placeholder="请输入发票税号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司地址" prop="companyAddress">
        <el-input
          v-model="queryParams.companyAddress"
          placeholder="请输入公司地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contacts">
        <el-input
          v-model="queryParams.contacts"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司电话" prop="companyPhone">
        <el-input
          v-model="queryParams.companyPhone"
          placeholder="请输入公司电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactsPhone">
        <el-input
          v-model="queryParams.contactsPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开户银行" prop="bankName">
        <el-input
          v-model="queryParams.bankName"
          placeholder="请输入开户银行"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行帐号" prop="bankAccount">
        <el-input
          v-model="queryParams.bankAccount"
          placeholder="请输入银行帐号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行行号" prop="bankNum">
        <el-input
          v-model="queryParams.bankNum"
          placeholder="请输入银行行号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择申请状态" clearable>
          <el-option
            v-for="dict in dict.type.c_apply_invoice"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          v-hasRole="['common', 'admin']"-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasRole="['common', 'admin']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasRole="['common', 'admin','dragon']"-->
<!--        >导出</el-button>-->
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="invoiceList" @selection-change="handleSelectionChange">
      <el-table-column label="发票类型" align="center" prop="invoiceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.c_invoice" :value="scope.row.invoiceType"/>
        </template>
      </el-table-column>
      <el-table-column label="公司名称" align="center" prop="companyName"/>
<!--      <el-table-column label="公司抬头名称" align="center" prop="invoiceTitle" />-->
      <el-table-column label="发票税号" align="center" prop="invoiceTaxNo"/>
<!--      <el-table-column label="发票金额" align="center" prop="invoiceAmount"/>-->
      <el-table-column label="开户银行" align="center" prop="bankName"/>
      <el-table-column label="银行帐号" align="center" prop="bankAccount"/>
      <el-table-column label="银行行号" align="center" prop="bankNum"/>
      <el-table-column label="公司地址" align="center" prop="companyAddress"/>
      <el-table-column label="联系人" align="center" prop="contacts"/>
      <el-table-column label="公司电话" align="center" prop="companyPhone"/>
      <el-table-column label="联系电话" align="center" prop="contactsPhone"/>
      <el-table-column label="申请状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.c_apply_invoice" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <!--      <el-table-column label="开票税金" align="center" prop="invoiceTax" />-->
      <!--      <el-table-column label="抬头名称" align="center" prop="invoiceTitle"/>-->
      <!--      <el-table-column label="抬头内容" align="center" prop="invoiceContent"/>-->
<!--      <el-table-column label="发票文件" align="center" prop="invoiceFile" >-->
<!--        <template slot-scope="scope">-->
<!--          <el-link @click="handleDownload(scope.row)" target="_blank" type="primary" icon="el-icon-download" >下载</el-link>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasRole="['common','admin','dragon']"-->
<!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasRole="['common','admin','dragon']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改发票对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="发票类型" prop="invoiceType">
          <el-radio-group v-model="form.invoiceType">
            <el-radio
              v-for="dict in dict.type.c_invoice"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>

                <el-form-item label="公司抬头名称" prop="invoiceTitle">
                  <el-input v-model="form.invoiceTitle" placeholder="请输入抬头名称"/>
                </el-form-item>
        <!--        <el-form-item label="抬头内容">-->
        <!--          <el-input type="textarea" v-model="form.invoiceContent" :min-height="192"/>-->
        <!--        </el-form-item>-->
<!--        <el-form-item label="发票金额" prop="invoiceAmount">-->
<!--          <el-input v-model="form.invoiceAmount" placeholder="请输入发票金额"/>-->
<!--        </el-form-item>-->
        <el-form-item label="发票税号" prop="invoiceTaxNo">
          <el-input v-model="form.invoiceTaxNo" placeholder="请输入发票税号"/>
        </el-form-item>
        <el-form-item label="公司地址" prop="companyAddress">
          <el-input v-model="form.companyAddress" placeholder="请输入公司地址"/>
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="form.contacts" placeholder="请输入联系人"/>
        </el-form-item>
        <el-form-item label="公司电话" prop="companyPhone">
          <el-input v-model="form.companyPhone" placeholder="请输入公司电话"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="contactsPhone">
          <el-input v-model="form.contactsPhone" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="开户银行" prop="bankName">
          <el-input v-model="form.bankName" placeholder="请输入开户银行"/>
        </el-form-item>
        <el-form-item label="银行帐号" prop="bankAccount">
          <el-input v-model="form.bankAccount" placeholder="请输入银行帐号"/>
        </el-form-item>
        <el-form-item label="银行行号" prop="bankNum">
          <el-input v-model="form.bankNum" placeholder="请输入银行行号"/>
        </el-form-item>
        <!--        <el-form-item label="开票税金" prop="invoiceTax">-->
        <!--          <el-input v-model="form.invoiceTax" placeholder="请输入开票税金"/>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getInvoice, delInvoice,  updateInvoice } from "@/api/consumer/invoice";
import {listInvoice, addInvoice} from '@/api/consumer/admin/invoice'

export default {
  name: "Invoice",
  dicts: ['c_invoice', 'c_apply_invoice'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发票表格数据
      invoiceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        companyId: null,
        userId: null,
        invoiceType: null,
        invoiceTitle: null,
        invoiceContent: null,
        invoiceAmount: null,
        invoiceTaxNo: null,
        invoiceTax: null,
        companyName: null,
        invoiceFile: null,
        companyAddress: null,
        contacts: null,
        companyPhone: null,
        contactsPhone: null,
        bankName: null,
        bankAccount: null,
        bankNum: null,
        isDelete: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        invoiceType: [
          {required: true, message: "发票类型不能为空", trigger: "change"}
        ],
        invoiceAmount: [
          {required: true, message: "发票金额不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "申请状态不能为空", trigger: "change"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 下载文件 */
// 文件下载处理
    handleDownload(row) {
      const a = document.createElement('a')
      var url = row.invoiceFile;
      a.setAttribute('href',  "https://zsky.lytx.net.cn" + url)
      a.click()
    },

    /** 查询发票列表 */
    getList() {
      this.loading = true;
      listInvoice(this.queryParams).then(response => {
        this.invoiceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        invoiceId: null,
        orderId: null,
        invoiceType: null,
        invoiceTitle: null,
        invoiceContent: null,
        invoiceAmount: null,
        invoiceTaxNo: null,
        invoiceTax: null,
        companyName: null,
        invoiceFile: null,
        companyAddress: null,
        contacts: null,
        companyPhone: null,
        bankName: null,
        bankAccount: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDelete: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.invoiceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加发票";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const invoiceId = row.invoiceId || this.ids
      getInvoice(invoiceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改发票";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.invoiceId != null) {
            updateInvoice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInvoice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const invoiceIds = row.invoiceId || this.ids;
      this.$modal.confirm('是否确认删除发票编号为"' + invoiceIds + '"的数据项？').then(function() {
        return delInvoice(invoiceIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('consumer/invoice/export', {
        ...this.queryParams
      }, `invoice_${new Date().getTime()}.xlsx`)
    },
    /** 开票按钮操作 */
    replayInvoice() {
          this.reset();
          this.open = true;
          this.title = "添加发票"
    },
  }
};
</script>
